[Code]
var
  bSysRestart: Boolean;
  DownloadPage: TDownloadWizardPage;

function OQSetupPath(Param: String): String;
var
  InstallPath: String;
  Names: TArrayOfString;
  I: Integer;
begin
  InstallPath := 'C:\Program Files\SmartQuant Ltd\OpenQuant 2014';
  if RegGetSubkeyNames(HKCR64, 'Installer\Assemblies', Names) then
  begin
    for I := 0 to GetArrayLength(Names)-1 do
    begin
      if Pos('SmartQuant.dll', Names[I]) > 0 then
      begin
        StringChangeEx(Names[I], '|', '\', True);
        InstallPath := ExtractFileDir(Names[I]);
        break;
      end;
    end;
  end;
  Result := InstallPath;
end;

function VCRedistIsNotInstalled: Boolean;
#ifdef InstallVCRedist
var
  RegItemKey: String;
  VcVersion: String;
#endif
begin
#ifdef InstallVCRedist  
  Result := True;
  RegItemKey := 'Installer\Dependencies\,,amd64,14.0,bundle'
  if RegValueExists(HKCR64, RegItemKey, 'Version') then
  begin
    RegQueryStringValue(HKCR64, RegItemKey, 'Version', VcVersion);
    Log(VcVersion);
    Result := not (VcVersion >= '14.10.25008.0');
  end;
#else
  Result := False;
#endif
end;

procedure InstallVCRedist;
var
  StatusText: string;
  VCRedist: string;
  ResultCode: Integer;
begin
  StatusText := WizardForm.StatusLabel.Caption;
  WizardForm.StatusLabel.Caption := 'Installing Microsoft VC++ 2017 redistributables. This might take a few minutes...';
  WizardForm.ProgressGauge.Style := npbstMarquee;
  try
    VCRedist := ExpandConstant('{tmp}\VCRedistInstaller.exe');
    Log(VCRedist);
    if not Exec(VCRedist, '/passive /norestart', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then
    begin
      MsgBox('Microsoft VC++ 2017 redistributables installation failed with code: ' + IntToStr(ResultCode) + '.', mbError, MB_OK);
    end;
  finally
    WizardForm.StatusLabel.Caption := StatusText;
    WizardForm.ProgressGauge.Style := npbstNormal;
    DeleteFile(ExpandConstant('{tmp}\VCRedistInstaller.exe'));
  end;
end;

procedure DetectRestart;
var
  sMsg: string;
  intResultCode: Integer;
begin
  if not bSysRestart then exit;
  sMsg := 'Setup must restart your computer to complete the installation.' + #13#10 + #13#10 + 'Would you like to restart now?';
  if SuppressibleMsgBox(sMsg, mbConfirmation, MB_YESNO, IDYES) = IDYES then
    Exec('shutdown.exe', '-r -t 0', '', SW_HIDE, ewNoWait, intResultCode);
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
  case CurStep of
    ssPostInstall:
      begin
        if not VCRedistIsNotInstalled then
          Exit;
        DownloadPage.Clear;
        DownloadPage.Add('https://aka.ms/vs/16/release/vc_redist.x64.exe', 'VCRedistInstaller.exe', '');
        DownloadPage.Show;
        try
          try
            DownloadPage.Download;            
          except
            SuppressibleMsgBox(AddPeriod(GetExceptionMessage), mbCriticalError, MB_OK, IDOK);
          end;
        finally
          DownloadPage.Hide;
        end;
        InstallVCRedist;        
      end;
    ssDone:
      begin
        DetectRestart;
      end;
  end;
end;

function OnDownloadProgress(const Url, FileName: String; const Progress, ProgressMax: Int64): Boolean;
begin
  if Progress = ProgressMax then
    Log(Format('Successfully downloaded file to {tmp}: %s', [FileName]));
  Result := True;
end;

procedure InitializeWizard;
begin
  DownloadPage := CreateDownloadPage(SetupMessage(msgWizardPreparing), SetupMessage(msgPreparingDesc), @OnDownloadProgress); 
end;